package com.topboya.linker.model;

import com.topboya.linker.model.levelitem.DepartmentGroupBo;
import com.topboya.linker.model.response.DeptUserResponse;
import com.topboya.linker.utils.LogUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Stack;

/**
 * Created by Administrator on 2019/1/22 0022.
 */

public class DataManagerbo {

     private static final String TAG = "DataManagerbo";
     private List<DeptUserResponse.Data> mData;
     List<DepartmentGroupBo> mCurrentGroups ;
     private Stack<List<DepartmentGroupBo>> mListStack;


    public DataManagerbo(List<DeptUserResponse.Data> mData){

          this.mData =mData;
          mListStack = new Stack<>();
          mCurrentGroups=new ArrayList<>();
      }

      public  List<DepartmentGroupBo> getCompanydata(){
          List<DepartmentGroupBo> mCurrentGroups  =new ArrayList<>();
          for (int i = 0; i < mData.size(); i++) {

              DepartmentGroupBo departmetGroup = new DepartmentGroupBo();
              DeptUserResponse.Data data = mData.get(i);
              departmetGroup.setMemberID(data.getId());
              departmetGroup.setDeparemtname(data.getName());
              departmetGroup.setSub(true);
              departmetGroup.setSelectedNum("0");
              departmetGroup.setSubDeptListss(data.getSubDeptList());
              departmetGroup.setDeptUserListss(data.getDeptUserList());
              departmetGroup.setDepartmentGroupBosPs(null);
              departmetGroup.setDepartmentGroupBoP(null);
              List<DepartmentGroupBo> children = generateChild(mCurrentGroups,departmetGroup,false);
              departmetGroup.setDepartmentGroupBosCd(children);
              mCurrentGroups.add(departmetGroup);

          }
          return mCurrentGroups;

      }

       public List<DepartmentGroupBo> next(int position,boolean checked) {

         mListStack.push(new ArrayList<>(mCurrentGroups));
         DepartmentGroupBo group = mCurrentGroups.get(position);
         mCurrentGroups.clear();
         mCurrentGroups.addAll(getNextData(group,checked));

        return mCurrentGroups;

     }

    private List<DepartmentGroupBo> getNextData(DepartmentGroupBo deptGroup,boolean checked) {

        List<DeptUserResponse.SubDeptList> cDeptList = deptGroup.getSubDeptListss();
        List<DeptUserResponse.DeptUserList> cUserList = deptGroup.getDeptUserListss();

        List<DepartmentGroupBo> result = new ArrayList<>();

        if (null != cDeptList) {
            for (int i = 0; i < cDeptList.size(); i++) {
                DepartmentGroupBo group = new DepartmentGroupBo();
                DeptUserResponse.SubDeptList list = cDeptList.get(i);
                group.setDeparemtname(list.getName());
                group.setMemberID(list.getId());
                group.setSub(true);
                group.setSelectedNum("0");
                group.setCheked(checked);
                group.setSubDeptListss(list.getSubDeptList());
                group.setDeptUserListss(list.getDeptUserList());
                result.add(group);
            }
        }
        if (null != cUserList) {
            for (int i = 0; i < cUserList.size(); i++) {

                DepartmentGroupBo group = new DepartmentGroupBo();
                DeptUserResponse.DeptUserList list = cUserList.get(i);
                group.setMemberID(list.getId());
                group.setDeparemtname(list.getName());
                group.setSub(false);
                group.setCheked(checked);
                result.add(group);
            }
        }
       LogUtils.v(TAG + "result:"+result.toString());
        return result;

    }

    public   List<DepartmentGroupBo> back() {

        if (mListStack.empty()) {
            return mCurrentGroups;
        }
        mCurrentGroups.clear();
        mCurrentGroups.addAll(mListStack.pop());

        return mCurrentGroups;
    }

        List<DepartmentGroupBo> departmentGroupsCildList = new ArrayList<>();
        List<DepartmentGroupBo> departmentGroupsCildListold = new ArrayList<>();

      public List<DepartmentGroupBo> getSelected(int position,List<DepartmentGroupBo> departmentGroupList,boolean ischeck){

            departmentGroupsCildListold.clear();
//          得到遍历到的所有孩子，拿到结果
            departmentGroupsCildListold.addAll(departmentGroupList);
            selectSelf(position,departmentGroupList);
            selectChild(departmentGroupList.get(position),ischeck);
            this.mCurrentGroups.clear();
            this.mCurrentGroups.addAll(departmentGroupsCildListold);
//         LogUtils.v(TAG + "departmentGroupList-----:"+this.departmentGroupList.toString());

          return departmentGroupsCildList;
      }


    public void  selectChild(DepartmentGroupBo group,boolean ischecked ) {

        List<DepartmentGroupBo>  dchild = getNextData(group,ischecked);
        if (null != dchild){

            LogUtils.v(TAG + "dchild:"+dchild.toString());
//            departmentGroupsCildList.addAll(dchild);
            for (int i=0;i < dchild.size();i++){

                DepartmentGroupBo item = dchild.get(i);
                selectChild(item,ischecked);
            }
        }
    }

    private void selectSelf(int position,List<DepartmentGroupBo> departmentGroupList ) {

        departmentGroupList.get(position).setCheked(true);

        departmentGroupsCildList.add(departmentGroupList.get(position));

    }
    private List<DepartmentGroupBo>  generateChild(List<DepartmentGroupBo> departmentGroupBospre,DepartmentGroupBo group,boolean ischecked ) {
        List<DepartmentGroupBo> departmentGroupBos = new ArrayList<>();
        List<DepartmentGroupBo>  dchild = getNextData(group,ischecked);
        if (null != dchild){

            LogUtils.v(TAG + "dchild:"+dchild.toString());
            departmentGroupBos.addAll(dchild);

            for (int i=0;i < dchild.size();i++){

                DepartmentGroupBo item = dchild.get(i);
                item.setDepartmentGroupBosPs(departmentGroupBospre);
                item.setDepartmentGroupBoP(group);
                List<DepartmentGroupBo> child = generateChild(dchild,item,ischecked);
                item.setDepartmentGroupBosCd(child);

            }
        }
        LogUtils.v(TAG + "departmentGroupBos:"+departmentGroupBos.toString());

        return departmentGroupBos;
    }

}
